<template>
	<view class="login">
		<view class="bg-white flex-sub radius shadow-lg" v-if="status">
			<image src="../../static/Hicon/loading-white.gif" mode="aspectFit" class="gif-white response" style="height:240upx"></image>
		</view>
		<!-- <radio-modal ref='radioModal'>sadasd</radio-modal> -->
	</view>
</template>

<script>
	import ls from '../../utils/local.js'
	import {
		login,mbLogin
	} from '../../api/service.js'
	import radioModal from '../basic/radio-modal'

	export default {
	
		data() {
			return {
				optionName: '--',
				//22017081IZER20240909
				userNo: '12007078',
				// userNo:'',
				realname: '--',
				status: false
			};
		},

		onLoad(option) {
			// localStorage.clear()
			// this.status = true;
			// // this.getUserInfor(option.token)
			// this.SSOLogin()
			var _this = this;
			localStorage.clear()
			// 测试环境放开这段代码
			console.log(this.userNo)
			// _this.userNo=option.userNo
			this.status=true;
			// _this.SSOLogin()
			// =====正式环境放开这段代码=========
			uni.request({
				url: '/wechat/wx/userNo/' + option.code,
				success: (res) => {
					_this.userNo = res.data;
					_this.SSOLogin()
				}
			})
		},

		onHide() {},
		onShow() {
			
			if (ls.get('mToken')) {
				uni.navigateTo({
					url: '../index/index'
				})
			} else {
				this.SSOLogin()
			}
		},
		methods: {
	// 		getUserInfor(token) {
	// 			alert('工号'+res.xgh)
	// 			let _this = this;
	// 			mbLogin({
	// 	          token:token
	// 			}).then(res=>{
	
	// 			  if(res.xgh){
	// 				  //+ 'IZER' + dayjs(new Date()).format('YYYYMMDD')
	// 				  _this.userNo=res.xgh + 'IZER' + dayjs(new Date()).format('YYYYMMDD')
	// 				  _this.SSOLogin()
	// 			  }else{
	// 				  uni.showToast({
	// 				  	title:'请退出系统重新登录！',
	// 				  	duration: 2000
	// 				  });
	// 			  }
				  
	// 			})
	// 		},
			  getFormattedDate() {
			      const date = new Date();
			      const year = date.getFullYear();
			      const month = String(date.getMonth() + 1).padStart(2, '0');
			      const day = String(date.getDate()).padStart(2, '0');
			      return `${year}${month}${day}`; // 格式化为 YYYY-MM-DD
			    },
			SSOLogin() {
				var _this = this;
				login({
					'userNo': this.userNo +'IZER' + this.getFormattedDate()
				}).then(res => {
					if (res.success) {
						ls.set('mUserInfo', res.result.userInfo);
						ls.set('mToken', res.result.token);
						ls.set('Listdeparts', res.result.departs)
						if (res.result.multi_depart == 2) {
							var currentDepart = res.result.departs.filter(item => {
								return item.orgCode == res.result.userInfo.orgCode
							})
							ls.set('departs', currentDepart[0].departName)
						} else {
							if (res.result.multi_depart == 1) {
								ls.set('departs', res.result.departs[0].departName)
							} else {
								ls.set('departs', '暂无')
							}

						}

						setTimeout(function() {
							uni.showToast({
								title: '欢迎登录成功！',
								duration: 2000,
								success: function() {
									_this.status = false
									uni.navigateTo({
										url: '../index/index'
									})
								}
							});

						}, 1200)

					} else {
						uni.showToast({
							title: res.message,
							duration: 2000
						});
					}
					
				})

			}
		}
	}
</script>

<style lang="scss">
	.login {
		// display: flex;
		justify-content: center;
		min-height: 100%;
		background: #fff;
		align-items: center;
		// line-height: 100%;
		position: absolute;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;

		.flex-sub {
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
			z-index: 10;
			width: 400rpx;
			height: auto;
		}
	}
</style>
